import { routes as constantRoutes } from '@/router/index';
import Layout from '@/layout/index'

console.log(constantRoutes,'3456')
const permission = {
    state: {
        routes: '',
        addRoutes: []
    },
    mutations: {
        SET_ROUTES: (state, routes) => {
            console.log(routes,'看看store里面的routes')
            state.addRoutes = routes
            state.routes = constantRoutes.concat(routes)
        }
    },
    actions: {
        GenerateRoutes({ commit }) {
            console.log('12121')
            return new Promise(resolve => {
                console.log('12121')
                const accessedRoutes = filterAsyncRouter(constantRoutes)
                accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
                commit('SET_ROUTES', accessedRoutes)
                resolve(accessedRoutes)
               
            })
        }
    }
}

function filterAsyncRouter(asyncRouterMap) {
    return asyncRouterMap.filter(route => {
        if (route.component) {
            if (route.component === 'Layout') {
                route.component = Layout
            } else {
                route.component = loadView(route.component)
            }
        }
        if (route.children != null && route.children && route.children.length) {
            route.children = filterAsyncRouter(route.children)
        }
        return true
    })
}

export const loadView = (view) => {
    return(resolve) => require([`@/views/${view}`], resolve)
}
export default permission